package laba1.task2;

import java.util.List;

/**
 *
 * @author yakimovich
 */
public class Perimeter {

    /**
     * @param figure
     * @return a perimeter of the Figure or -1 if perimeter can not be
     * calculated
     */
    public static double perimeter(Figure figure) {
        double result = 0.0;
        if (figure == null) {
            return -1;
        }
        List<Point> points = figure.getList();

        for (int i = 0; i < points.size() - 1; i++) {
            Point start = points.get(i);
            Point end = points.get(i + 1);
            result = (start != null && end != null) ? result + start.distanceTo(end) : -1;
        } 

        Point start = points.get(0);
        Point end = points.get(points.size() - 1);
        result = (start != null && end != null) ? result + start.distanceTo(end) : -1;
        return result;
    }
}
